/*=====================================================================================
   Program Name: ecoline
   This is a program that draws nonlinear lines in a multi-party setting
   from from binary relationships using base programs such as ecol4.  

Program written by Won-ho Park, University of Michigan
Copyright Won-ho Park, Sept. 2002 
=======================================================================================
  The input arguments are two sets of vote returns (which means not fractions),
  the first set in the variable list, and the other in the "Second" argument.  Thus, the command
  syntax would be 

  ecoline party1_election1 party2_election1 ... , Second(party1_election2 party2_election2...)
  and other options that are parallel to STATA graph options. 
========================================================================================*/

program define ecoline, rclass byable(recall)
	version 7.0
	syntax varlist(min=2 numeric) [if] [in] , SECond(varlist numeric min=2) [SAVing(passthru) NONLINear(passthru) *]
		if ("`saving'"~=""){
		di "{p 0 0 30} {error: Please save the graph separately after it is drawn. The saving function is reserved for compounding multiple figures.}{p_end}"
		exit 198}
	marksample touse, strok
	tempname T T2 Corr nofp_1 nofp_2 natsum1 natsum2 tot1 total natsum22
	* Here, store the list of the 1st parties in local macros, "pi_e1"
	tokenize `varlist'
	local i=1
	while "``i''"~="" {
		local p`i'_e1  "``i''"
*		di "`p`i'_e1'"
		local i = `i' + 1
	}
	scalar `nofp_1' = `i'-1
*	di `nofp_1'

	* Now, store the list of the 2nd election parties in local macros, "pi_e2"
	tokenize `second'
	local i=1
	while "``i''"~="" {
		local p`i'_e2  "``i''"
*		di "`p`i'_e2'"
		local i = `i' + 1
	}
	scalar `nofp_2' = `i' - 1
*	di `nofp_2'

	tempvar total1 total2
	qui egen `total1'= rsum(`varlist')
	qui egen `total2'= rsum(`second')

	* Now run r by c times of binary pplines: 
local c = `nofp_1'
local r = `nofp_2'
local fignum = 1
local figlist
di "Do you want {ul:a}ll the pictures, or {ul:o}ne at a time? Type either <o> or <a>" _request(figopt)
local i = 0
while (`i'==0){
	if ("$figopt"=="a"){
		set textsize 200
		forvalues j = 1/`r'{
			forvalues i = 1/`c'{
			ppline02 `p`i'_e1' `total1' `p`j'_e2' `total2' if `touse', saving(ecolinefigures`fignum', replace) `nonlinear' `options' 
			local figlist "`figlist' ecolinefigures`fignum' "
			local fignum = `fignum' + 1 
			}
		}
		set textsize 100
		gr using `figlist'
		shell del ecolinefigures*.gph
*		turn 
		local i = 1
	}
	else if ("$figopt"=="o"){
		set more on
		forvalues j = 1/`r'{
			forvalues i = 1/`c'{
			ppline02 `p`i'_e1' `total1' `p`j'_e2' `total2' if `touse', `nonlinear' `options'
			di "{it: Press Any Key}"
			more
			}
		}
	local i = 1
	}
	else{
		di "Type either {it:a} or {it:o} " _request (figopt)
	}
}
macro drop figopt
end

program define ppline02, rclass byable(recall)
  version 7.0
syntax varlist(min=4 max=4 numeric) [if] [in][, NONLINear(string) * ]
	marksample touse , strok
	tokenize `varlist'
	confirm var `1' `2' `3' `4'
	local vote1 `1' 
	local vote2 `2' 
	local vote3 `3' 
	local vote4 `4' 
	**********************************
	* Declare Temp variables, macros, matrices
	tempname natsum nat_v1 nat_v2 

	***********************************
	* Non-linear Tranformation
	tempvar x1 x2 hat px1 px2 phat pihat lx1 lx2 lhat lihat

	quietly {
		gen `x1'=`vote1'/`vote2' if `touse' 
		gen `x2'=`vote3'/`vote4' if `touse'
		regress `x2' `x1'
		predict `hat'
		label variable `hat' "Linear Fit"
		gen `px1'=invnorm(`x1') if (`touse')
		gen `px2'=invnorm(`x2') if (`touse')
		regress `px2' `px1'
		predict `phat'
		gen `pihat'=norm(`phat')
		label variable `pihat' "Probit Fit"
		gen `lx1'= log(`vote1'/(`vote2'-`vote1')) if `touse'
		gen `lx2'= log(`vote3'/(`vote4'-`vote3')) if `touse'
		regress `lx2' `lx1'
		predict `lhat'
		gen `lihat'=exp(`lhat')/(1 + exp(`lhat'))
		label variable `lihat' "Logit Fit"
	}

* Now draw the graph: 

if "`nonlinear'"=="linear" {
	capture gr `x2' `hat' `x1', s(.ii) c(.ll) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
else if "`nonlinear'"=="all" {
	capture gr `x2' `hat' `pihat' `lihat' `x1', s(.iii) c(.lll[_]) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
else if "`nonlinear'"=="probit" {
	capture gr `x2' `pihat' `lihat' `x1', s(.ii) c(.ll) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
else if "`nonlinear'"=="logit" {
	capture gr `x2' `hat' `lihat' `x1', s(.ii) c(.ll) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
else if "`nonlinear'"=="logitonly"{
	capture gr `x2' `lihat' `x1', s(.i) c(.l) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
else if "`nonlinear'"=="probitonly"{
	capture gr `x2' `pihat' `x1', s(.i) c(.l) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
else if "`nonlinear'"=="probitonly"{
	capture gr `x2' `pihat' `x1', s(.i) c(.l) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
else if "`nonlinear'"=="probitandlogit"{
	capture gr `x2' `lihat' `pihat' `x1', s(.ii) c(.ll) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
else {
	capture gr `x2' `hat' `pihat' `x1', s(.ii) c(.ll[_]) b2("`vote1'") l1("`vote3'") sort `saving' `options'
}
end
